import { ParameterizedContext } from 'koa';
import { getManager, Repository } from 'typeorm';
import { TcUsers } from '../models/users';
import { TcContents } from '../models/contents';
export const addUsers = async (ctx: ParameterizedContext) => {
  const user: TcUsers = new TcUsers();
  user.name = 'jayken';
  user.email = 'jaykenxie@qq.com';
  user.passwd = '1234561';
  const userRepository: Repository<TcUsers> = getManager().getRepository(TcUsers);
  const res = await userRepository.manager.save(user);
  ctx.body = res;
};

export const getUsers = async (ctx: ParameterizedContext) => {
  const userRepository: Repository<TcUsers> = getManager().getRepository(TcUsers);

  const users: TcUsers[] = await userRepository.find();
  ctx.body = users;
};

// Contents
export const getContents = async (ctx: ParameterizedContext) => {
  const contentRepository: Repository<TcContents> = getManager().getRepository(
    TcContents,
  );

  const contents: TcContents[] = await contentRepository.find();
  ctx.body = contents;
};

export const viewIndex = async (ctx: ParameterizedContext) => {
  const userRepository: Repository<TcUsers> = getManager().getRepository(TcUsers);
  const users: TcUsers[] = await userRepository.find();
  await ctx.render('index', {
    title: 'hello koa',
    users,
  });
};
